

<!DOCTYPE html>
<html class="writer-html5" lang="zh" >
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>项目 &mdash; Scrapy 2.3.0 文档</title>
  

  
  <link rel="stylesheet" href="../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="../_static/css/tooltipster.custom.css" type="text/css" />
  <link rel="stylesheet" href="../_static/css/tooltipster.bundle.min.css" type="text/css" />
  <link rel="stylesheet" href="../_static/css/tooltipster-sideTip-shadow.min.css" type="text/css" />
  <link rel="stylesheet" href="../_static/css/tooltipster-sideTip-punk.min.css" type="text/css" />
  <link rel="stylesheet" href="../_static/css/tooltipster-sideTip-noir.min.css" type="text/css" />
  <link rel="stylesheet" href="../_static/css/tooltipster-sideTip-light.min.css" type="text/css" />
  <link rel="stylesheet" href="../_static/css/tooltipster-sideTip-borderless.min.css" type="text/css" />
  <link rel="stylesheet" href="../_static/css/micromodal.css" type="text/css" />
  <link rel="stylesheet" href="../_static/css/sphinx_rtd_theme.css" type="text/css" />

  
  
  
  

  
  <!--[if lt IE 9]>
    <script src="../_static/js/html5shiv.min.js"></script>
  <![endif]-->
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
        <script src="../_static/jquery.js"></script>
        <script src="../_static/underscore.js"></script>
        <script src="../_static/doctools.js"></script>
        <script src="../_static/language_data.js"></script>
        <script src="../_static/js/hoverxref.js"></script>
        <script src="../_static/js/tooltipster.bundle.min.js"></script>
        <script src="../_static/js/micromodal.min.js"></script>
    
    <script type="text/javascript" src="../_static/js/theme.js"></script>

    
    <link rel="index" title="索引" href="../genindex.html" />
    <link rel="search" title="搜索" href="../search.html" />
    <link rel="next" title="项目加载器" href="loaders.html" />
    <link rel="prev" title="选择器" href="selectors.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../index.html" class="icon icon-home" alt="Documentation Home"> Scrapy
          

          
          </a>

          
            
            
              <div class="version">
                2.3
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        
        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">第一步</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../intro/overview.html">Scrapy一目了然</a></li>
<li class="toctree-l1"><a class="reference internal" href="../intro/install.html">安装指南</a></li>
<li class="toctree-l1"><a class="reference internal" href="../intro/tutorial.html">Scrapy 教程</a></li>
<li class="toctree-l1"><a class="reference internal" href="../intro/examples.html">实例</a></li>
</ul>
<p class="caption"><span class="caption-text">基本概念</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="commands.html">命令行工具</a></li>
<li class="toctree-l1"><a class="reference internal" href="spiders.html">蜘蛛</a></li>
<li class="toctree-l1"><a class="reference internal" href="selectors.html">选择器</a></li>
<li class="toctree-l1 current"><a class="current reference internal" href="#">项目</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#item-types">项目类型</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#dictionaries">辞典</a></li>
<li class="toctree-l3"><a class="reference internal" href="#item-objects">项目对象</a></li>
<li class="toctree-l3"><a class="reference internal" href="#dataclass-objects">数据类对象</a></li>
<li class="toctree-l3"><a class="reference internal" href="#attr-s-objects">属性对象</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="#working-with-item-objects">使用项目对象</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#declaring-item-subclasses">声明项子类</a></li>
<li class="toctree-l3"><a class="reference internal" href="#declaring-fields">声明字段</a></li>
<li class="toctree-l3"><a class="reference internal" href="#id3">使用项目对象</a><ul>
<li class="toctree-l4"><a class="reference internal" href="#creating-items">创建项目</a></li>
<li class="toctree-l4"><a class="reference internal" href="#getting-field-values">获取字段值</a></li>
<li class="toctree-l4"><a class="reference internal" href="#setting-field-values">设置字段值</a></li>
<li class="toctree-l4"><a class="reference internal" href="#accessing-all-populated-values">访问所有填充的值</a></li>
<li class="toctree-l4"><a class="reference internal" href="#copying-items">复制项目</a></li>
<li class="toctree-l4"><a class="reference internal" href="#other-common-tasks">其他常见任务</a></li>
</ul>
</li>
<li class="toctree-l3"><a class="reference internal" href="#extending-item-subclasses">扩展项子类</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="#supporting-all-item-types">支持所有项目类型</a></li>
<li class="toctree-l2"><a class="reference internal" href="#other-classes-related-to-items">与项目相关的其他类别</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="loaders.html">项目加载器</a></li>
<li class="toctree-l1"><a class="reference internal" href="shell.html">Scrapy shell</a></li>
<li class="toctree-l1"><a class="reference internal" href="item-pipeline.html">项目管道</a></li>
<li class="toctree-l1"><a class="reference internal" href="feed-exports.html">Feed 导出</a></li>
<li class="toctree-l1"><a class="reference internal" href="request-response.html">请求和响应</a></li>
<li class="toctree-l1"><a class="reference internal" href="link-extractors.html">链接提取器</a></li>
<li class="toctree-l1"><a class="reference internal" href="settings.html">设置</a></li>
<li class="toctree-l1"><a class="reference internal" href="exceptions.html">例外情况</a></li>
</ul>
<p class="caption"><span class="caption-text">内置服务</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="logging.html">登录</a></li>
<li class="toctree-l1"><a class="reference internal" href="stats.html">统计数据集合</a></li>
<li class="toctree-l1"><a class="reference internal" href="email.html">发送电子邮件</a></li>
<li class="toctree-l1"><a class="reference internal" href="telnetconsole.html">远程登录控制台</a></li>
<li class="toctree-l1"><a class="reference internal" href="webservice.html">Web服务</a></li>
</ul>
<p class="caption"><span class="caption-text">解决具体问题</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../faq.html">常见问题</a></li>
<li class="toctree-l1"><a class="reference internal" href="debug.html">调试spiders</a></li>
<li class="toctree-l1"><a class="reference internal" href="contracts.html">蜘蛛合约</a></li>
<li class="toctree-l1"><a class="reference internal" href="practices.html">常用做法</a></li>
<li class="toctree-l1"><a class="reference internal" href="broad-crawls.html">宽爬行</a></li>
<li class="toctree-l1"><a class="reference internal" href="developer-tools.html">使用浏览器的开发人员工具进行抓取</a></li>
<li class="toctree-l1"><a class="reference internal" href="dynamic-content.html">选择动态加载的内容</a></li>
<li class="toctree-l1"><a class="reference internal" href="leaks.html">调试内存泄漏</a></li>
<li class="toctree-l1"><a class="reference internal" href="media-pipeline.html">下载和处理文件和图像</a></li>
<li class="toctree-l1"><a class="reference internal" href="deploy.html">部署蜘蛛</a></li>
<li class="toctree-l1"><a class="reference internal" href="autothrottle.html">AutoThrottle 扩展</a></li>
<li class="toctree-l1"><a class="reference internal" href="benchmarking.html">标杆管理</a></li>
<li class="toctree-l1"><a class="reference internal" href="jobs.html">作业：暂停和恢复爬行</a></li>
<li class="toctree-l1"><a class="reference internal" href="coroutines.html">协同程序</a></li>
<li class="toctree-l1"><a class="reference internal" href="asyncio.html">asyncio</a></li>
</ul>
<p class="caption"><span class="caption-text">扩展Scrapy</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="architecture.html">体系结构概述</a></li>
<li class="toctree-l1"><a class="reference internal" href="downloader-middleware.html">下载器中间件</a></li>
<li class="toctree-l1"><a class="reference internal" href="spider-middleware.html">蜘蛛中间件</a></li>
<li class="toctree-l1"><a class="reference internal" href="extensions.html">扩展</a></li>
<li class="toctree-l1"><a class="reference internal" href="api.html">核心API</a></li>
<li class="toctree-l1"><a class="reference internal" href="signals.html">信号</a></li>
<li class="toctree-l1"><a class="reference internal" href="exporters.html">条目导出器</a></li>
</ul>
<p class="caption"><span class="caption-text">其余所有</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../news.html">发行说明</a></li>
<li class="toctree-l1"><a class="reference internal" href="../contributing.html">为 Scrapy 贡献</a></li>
<li class="toctree-l1"><a class="reference internal" href="../versioning.html">版本控制和API稳定性</a></li>
</ul>

            
          
        </div>
        
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../index.html">Scrapy</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../index.html" class="icon icon-home"></a> &raquo;</li>
        
      <li>项目</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
            
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <div class="section" id="module-scrapy.item">
<span id="items"></span><span id="topics-items"></span><h1>项目<a class="headerlink" href="#module-scrapy.item" title="永久链接至标题">¶</a></h1>
<p>剪贴的主要目标是从非结构化源（通常是网页）中提取结构化数据。 <a class="reference internal" href="spiders.html#topics-spiders"><span class="std std-ref">Spiders</span></a> 将提取的数据返回为 <cite>items</cite> ，定义键-值对的Python对象。</p>
<p>支点 <a class="reference internal" href="#item-types"><span class="std std-ref">multiple types of items</span></a> . 创建项目时，可以使用所需的任何类型的项目。当您编写接收项目的程式码时，您的程式码应该 <a class="reference internal" href="#supporting-item-types"><span class="std std-ref">work for any item type</span></a> .</p><script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle"
     style="display:block; text-align:center;"
     data-ad-layout="in-article"
     data-ad-format="fluid"
     data-ad-client="ca-pub-1466963416408457"
     data-ad-slot="8850786025"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
<div class="section" id="item-types">
<span id="id1"></span><h2>项目类型<a class="headerlink" href="#item-types" title="永久链接至标题">¶</a></h2>
<p>Scrapy支持以下类型的项目，通过 <a class="reference external" href="https://github.com/scrapy/itemadapter">itemadapter</a> 类库： <a class="reference internal" href="#dict-items"><span class="std std-ref">dictionaries</span></a> ， <a class="reference internal" href="#item-objects"><span class="std std-ref">Item objects</span></a> ， <a class="reference internal" href="#dataclass-items"><span class="std std-ref">dataclass objects</span></a> 和 <a class="reference internal" href="#attrs-items"><span class="std std-ref">attrs objects</span></a> .</p>
<div class="section" id="dictionaries">
<span id="dict-items"></span><h3>辞典<a class="headerlink" href="#dictionaries" title="永久链接至标题">¶</a></h3>
<p>作为项目类型， <a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#dict" title="(在 Python v3.9)"><code class="xref py py-class docutils literal notranslate"><span class="pre">dict</span></code></a> 方便又熟悉。</p>
</div>
<div class="section" id="item-objects">
<span id="id2"></span><h3>项目对象<a class="headerlink" href="#item-objects" title="永久链接至标题">¶</a></h3>
<p><a class="reference internal" href="#scrapy.item.Item" title="scrapy.item.Item"><code class="xref py py-class docutils literal notranslate"><span class="pre">Item</span></code></a> 提供了一个 <a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#dict" title="(在 Python v3.9)"><code class="xref py py-class docutils literal notranslate"><span class="pre">dict</span></code></a> -像API加上其他功能，使其成为功能最齐全的项目类型：</p>
<dl class="py class">
<dt id="scrapy.item.Item">
<em class="property">class </em><code class="sig-prename descclassname">scrapy.item.</code><code class="sig-name descname">Item</code><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param">arg</em><span class="optional">]</span><span class="sig-paren">)</span><a class="reference internal" href="../_modules/scrapy/item.html#Item"><span class="viewcode-link">[源代码]</span></a><a class="headerlink" href="#scrapy.item.Item" title="永久链接至目标">¶</a></dt>
<dd><p><a class="reference internal" href="#scrapy.item.Item" title="scrapy.item.Item"><code class="xref py py-class docutils literal notranslate"><span class="pre">Item</span></code></a> 对象复制标准 <a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#dict" title="(在 Python v3.9)"><code class="xref py py-class docutils literal notranslate"><span class="pre">dict</span></code></a> API，包括其 <code class="docutils literal notranslate"><span class="pre">__init__</span></code> 方法。</p>
<p><a class="reference internal" href="#scrapy.item.Item" title="scrapy.item.Item"><code class="xref py py-class docutils literal notranslate"><span class="pre">Item</span></code></a> 允许定义字段名，以便：</p>
<ul class="simple">
<li><p><a class="reference external" href="https://docs.python.org/3/library/exceptions.html#KeyError" title="(在 Python v3.9)"><code class="xref py py-class docutils literal notranslate"><span class="pre">KeyError</span></code></a> 在使用未定义的字段名时引发（即防止打字错误被忽略）</p></li>
<li><p><a class="reference internal" href="exporters.html#topics-exporters"><span class="std std-ref">Item exporters</span></a> 默认情况下可以导出所有字段，即使第一个刮取的对象没有所有字段的值</p></li>
</ul>
<p><a class="reference internal" href="#scrapy.item.Item" title="scrapy.item.Item"><code class="xref py py-class docutils literal notranslate"><span class="pre">Item</span></code></a> 还允许定义字段元数据，可用于 <a class="reference internal" href="exporters.html#topics-exporters-field-serialization"><span class="std std-ref">customize serialization</span></a> .</p>
<p><code class="xref py py-mod docutils literal notranslate"><span class="pre">trackref</span></code> 轨道 <a class="reference internal" href="#scrapy.item.Item" title="scrapy.item.Item"><code class="xref py py-class docutils literal notranslate"><span class="pre">Item</span></code></a> 对象来帮助查找内存泄漏（请参见 <a class="reference internal" href="leaks.html#topics-leaks-trackrefs"><span class="std std-ref">使用调试内存泄漏 trackref</span></a> ）</p>
<p><a class="reference internal" href="#scrapy.item.Item" title="scrapy.item.Item"><code class="xref py py-class docutils literal notranslate"><span class="pre">Item</span></code></a> 对象还提供以下附加API成员：</p>
<dl class="py method">
<dt id="scrapy.item.Item.copy">
<code class="sig-name descname">copy</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#scrapy.item.Item.copy" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<dl class="py method">
<dt id="scrapy.item.Item.deepcopy">
<code class="sig-name descname">deepcopy</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#scrapy.item.Item.deepcopy" title="永久链接至目标">¶</a></dt>
<dd><p>返回A <a class="reference external" href="https://docs.python.org/3/library/copy.html#copy.deepcopy" title="(在 Python v3.9)"><code class="xref py py-func docutils literal notranslate"><span class="pre">deepcopy()</span></code></a> 这个项目的。</p>
</dd></dl>

<dl class="py attribute">
<dt id="scrapy.item.Item.fields">
<code class="sig-name descname">fields</code><a class="headerlink" href="#scrapy.item.Item.fields" title="永久链接至目标">¶</a></dt>
<dd><p>包含 <em>所有已声明的字段</em> 对于这个项目，不仅仅是那些填充的。键是字段名，值是 <a class="reference internal" href="#scrapy.item.Field" title="scrapy.item.Field"><code class="xref py py-class docutils literal notranslate"><span class="pre">Field</span></code></a> 中使用的对象 <a class="reference internal" href="#topics-items-declaring"><span class="std std-ref">Item declaration</span></a> .</p>
</dd></dl>

</dd></dl>

<p>例子：：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">scrapy.item</span> <span class="kn">import</span> <span class="n">Item</span><span class="p">,</span> <span class="n">Field</span>

<span class="k">class</span> <span class="nc">CustomItem</span><span class="p">(</span><span class="n">Item</span><span class="p">):</span>
    <span class="n">one_field</span> <span class="o">=</span> <span class="n">Field</span><span class="p">()</span>
    <span class="n">another_field</span> <span class="o">=</span> <span class="n">Field</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="section" id="dataclass-objects">
<span id="dataclass-items"></span><h3>数据类对象<a class="headerlink" href="#dataclass-objects" title="永久链接至标题">¶</a></h3>
<div class="versionadded">
<p><span class="versionmodified added">2.2 新版功能.</span></p>
</div>
<p><a class="reference external" href="https://docs.python.org/3/library/dataclasses.html#dataclasses.dataclass" title="(在 Python v3.9)"><code class="xref py py-func docutils literal notranslate"><span class="pre">dataclass()</span></code></a> 允许使用字段名定义项类，以便 <a class="reference internal" href="exporters.html#topics-exporters"><span class="std std-ref">item exporters</span></a> 默认情况下可以导出所有字段，即使第一个刮取的对象没有所有字段的值。</p>
<p>此外， <code class="docutils literal notranslate"><span class="pre">dataclass</span></code> 项目还允许：</p>
<ul class="simple">
<li><p>定义每个定义字段的类型和默认值。</p></li>
<li><p>通过定义自定义字段元数据 <a class="reference external" href="https://docs.python.org/3/library/dataclasses.html#dataclasses.field" title="(在 Python v3.9)"><code class="xref py py-func docutils literal notranslate"><span class="pre">dataclasses.field()</span></code></a> ，可以用来 <a class="reference internal" href="exporters.html#topics-exporters-field-serialization"><span class="std std-ref">customize serialization</span></a> .</p></li>
</ul>
<p>它们在python3.7或更高版本中以本机方式工作，或者使用 <a class="reference external" href="https://pypi.org/project/dataclasses/">dataclasses backport</a> 在Python 3.6中。</p>
<p>例子：：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">dataclasses</span> <span class="kn">import</span> <span class="n">dataclass</span>

<span class="nd">@dataclass</span>
<span class="k">class</span> <span class="nc">CustomItem</span><span class="p">:</span>
    <span class="n">one_field</span><span class="p">:</span> <span class="nb">str</span>
    <span class="n">another_field</span><span class="p">:</span> <span class="nb">int</span>
</pre></div>
</div>
<div class="admonition note">
<p class="admonition-title">注解</p>
<p>在运行时不强制使用字段类型。</p>
</div>
</div>
<div class="section" id="attr-s-objects">
<span id="attrs-items"></span><h3>属性对象<a class="headerlink" href="#attr-s-objects" title="永久链接至标题">¶</a></h3>
<div class="versionadded">
<p><span class="versionmodified added">2.2 新版功能.</span></p>
</div>
<p><a class="reference external" href="https://www.attrs.org/en/stable/api.html#attr.s" title="(在 attrs v20.2)"><code class="xref py py-func docutils literal notranslate"><span class="pre">attr.s()</span></code></a> 允许使用字段名定义项类，以便 <a class="reference internal" href="exporters.html#topics-exporters"><span class="std std-ref">item exporters</span></a> 默认情况下可以导出所有字段，即使第一个刮取的对象没有所有字段的值。</p>
<p>此外， <code class="docutils literal notranslate"><span class="pre">attr.s</span></code> 项目还允许：</p>
<ul class="simple">
<li><p>定义每个定义字段的类型和默认值。</p></li>
<li><p>定义自定义字段 <a class="reference external" href="https://www.attrs.org/en/stable/examples.html#metadata" title="(在 attrs v20.2)"><span class="xref std std-ref">metadata</span></a> ，可以用来 <a class="reference internal" href="exporters.html#topics-exporters-field-serialization"><span class="std std-ref">customize serialization</span></a> .</p></li>
</ul>
<p>为了使用此类型 <a class="reference external" href="https://www.attrs.org/en/stable/index.html" title="(在 attrs v20.2)"><span class="xref std std-doc">attrs package</span></a> 需要安装。</p>
<p>例子：：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">attr</span>

<span class="nd">@attr</span><span class="o">.</span><span class="n">s</span>
<span class="k">class</span> <span class="nc">CustomItem</span><span class="p">:</span>
    <span class="n">one_field</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">()</span>
    <span class="n">another_field</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">()</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="working-with-item-objects">
<h2>使用项目对象<a class="headerlink" href="#working-with-item-objects" title="永久链接至标题">¶</a></h2>
<div class="section" id="declaring-item-subclasses">
<span id="topics-items-declaring"></span><h3>声明项子类<a class="headerlink" href="#declaring-item-subclasses" title="永久链接至标题">¶</a></h3>
<p>项子类使用简单的类定义语法和 <a class="reference internal" href="#scrapy.item.Field" title="scrapy.item.Field"><code class="xref py py-class docutils literal notranslate"><span class="pre">Field</span></code></a> 物体。下面是一个例子：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">scrapy</span>

<span class="k">class</span> <span class="nc">Product</span><span class="p">(</span><span class="n">scrapy</span><span class="o">.</span><span class="n">Item</span><span class="p">):</span>
    <span class="n">name</span> <span class="o">=</span> <span class="n">scrapy</span><span class="o">.</span><span class="n">Field</span><span class="p">()</span>
    <span class="n">price</span> <span class="o">=</span> <span class="n">scrapy</span><span class="o">.</span><span class="n">Field</span><span class="p">()</span>
    <span class="n">stock</span> <span class="o">=</span> <span class="n">scrapy</span><span class="o">.</span><span class="n">Field</span><span class="p">()</span>
    <span class="n">tags</span> <span class="o">=</span> <span class="n">scrapy</span><span class="o">.</span><span class="n">Field</span><span class="p">()</span>
    <span class="n">last_updated</span> <span class="o">=</span> <span class="n">scrapy</span><span class="o">.</span><span class="n">Field</span><span class="p">(</span><span class="n">serializer</span><span class="o">=</span><span class="nb">str</span><span class="p">)</span>
</pre></div>
</div>
<div class="admonition note">
<p class="admonition-title">注解</p>
<p>那些熟悉 <a class="reference external" href="https://www.djangoproject.com/">Django</a> 会注意到报废物品的声明类似于 <a class="reference external" href="https://docs.djangoproject.com/en/dev/topics/db/models/">Django Models</a> 但是，由于不存在不同字段类型的概念，因此片段项要简单得多。</p>
</div>
</div>
<div class="section" id="declaring-fields">
<span id="topics-items-fields"></span><h3>声明字段<a class="headerlink" href="#declaring-fields" title="永久链接至标题">¶</a></h3>
<p><a class="reference internal" href="#scrapy.item.Field" title="scrapy.item.Field"><code class="xref py py-class docutils literal notranslate"><span class="pre">Field</span></code></a> 对象用于为每个字段指定元数据。例如，用于 <code class="docutils literal notranslate"><span class="pre">last_updated</span></code> 上面示例中所示的字段。</p>
<p>可以为每个字段指定任何类型的元数据。对接受的值没有限制 <a class="reference internal" href="#scrapy.item.Field" title="scrapy.item.Field"><code class="xref py py-class docutils literal notranslate"><span class="pre">Field</span></code></a> 物体。出于同样的原因，没有所有可用元数据键的引用列表。中定义的每个键 <a class="reference internal" href="#scrapy.item.Field" title="scrapy.item.Field"><code class="xref py py-class docutils literal notranslate"><span class="pre">Field</span></code></a> 对象可以由不同的组件使用，只有那些组件知道它。您还可以定义和使用任何其他 <a class="reference internal" href="#scrapy.item.Field" title="scrapy.item.Field"><code class="xref py py-class docutils literal notranslate"><span class="pre">Field</span></code></a> 为了你自己的需要，也要输入你的项目。的主要目标 <a class="reference internal" href="#scrapy.item.Field" title="scrapy.item.Field"><code class="xref py py-class docutils literal notranslate"><span class="pre">Field</span></code></a> 对象是提供一种在一个地方定义所有字段元数据的方法。通常，行为依赖于每个字段的组件使用特定的字段键来配置该行为。您必须参考它们的文档来查看每个组件使用的元数据键。</p>
<p>重要的是要注意 <a class="reference internal" href="#scrapy.item.Field" title="scrapy.item.Field"><code class="xref py py-class docutils literal notranslate"><span class="pre">Field</span></code></a> 用于声明该项的对象不会保留分配为类属性的状态。相反，可以通过 <a class="reference internal" href="#scrapy.item.Item.fields" title="scrapy.item.Item.fields"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Item.fields</span></code></a> 属性。</p>
<dl class="py class">
<dt id="scrapy.item.Field">
<em class="property">class </em><code class="sig-prename descclassname">scrapy.item.</code><code class="sig-name descname">Field</code><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param">arg</em><span class="optional">]</span><span class="sig-paren">)</span><a class="reference internal" href="../_modules/scrapy/item.html#Field"><span class="viewcode-link">[源代码]</span></a><a class="headerlink" href="#scrapy.item.Field" title="永久链接至目标">¶</a></dt>
<dd><p>这个 <a class="reference internal" href="#scrapy.item.Field" title="scrapy.item.Field"><code class="xref py py-class docutils literal notranslate"><span class="pre">Field</span></code></a> 类只是内置的别名 <a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#dict" title="(在 Python v3.9)"><code class="xref py py-class docutils literal notranslate"><span class="pre">dict</span></code></a> 类，不提供任何额外的功能或属性。换言之， <a class="reference internal" href="#scrapy.item.Field" title="scrapy.item.Field"><code class="xref py py-class docutils literal notranslate"><span class="pre">Field</span></code></a> 对象是普通的旧python dict。单独的类用于支持 <a class="reference internal" href="#topics-items-declaring"><span class="std std-ref">item declaration syntax</span></a> 基于类属性。</p>
</dd></dl>

<div class="admonition note">
<p class="admonition-title">注解</p>
<p>也可以为声明字段元数据 <code class="docutils literal notranslate"><span class="pre">dataclass</span></code> 和 <code class="docutils literal notranslate"><span class="pre">attrs</span></code> 项目。请参考文件 <a class="reference external" href="https://docs.python.org/3/library/dataclasses.html#dataclasses.field">dataclasses.field</a> 和 <a class="reference external" href="https://www.attrs.org/en/stable/api.html#attr.ib">attr.ib</a> 更多信息。</p>
</div>
</div>
<div class="section" id="id3">
<h3>使用项目对象<a class="headerlink" href="#id3" title="永久链接至标题">¶</a></h3>
<p>下面是一些使用项执行的常见任务的示例，使用 <code class="docutils literal notranslate"><span class="pre">Product</span></code> 项目 <a class="reference internal" href="#topics-items-declaring"><span class="std std-ref">declared above</span></a> . 您会注意到API与 <a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#dict" title="(在 Python v3.9)"><code class="xref py py-class docutils literal notranslate"><span class="pre">dict</span></code></a> 应用程序编程接口。</p>
<div class="section" id="creating-items">
<h4>创建项目<a class="headerlink" href="#creating-items" title="永久链接至标题">¶</a></h4>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">product</span> <span class="o">=</span> <span class="n">Product</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s1">&#39;Desktop PC&#39;</span><span class="p">,</span> <span class="n">price</span><span class="o">=</span><span class="mi">1000</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">product</span><span class="p">)</span>
<span class="go">Product(name=&#39;Desktop PC&#39;, price=1000)</span>
</pre></div>
</div>
</div>
<div class="section" id="getting-field-values">
<h4>获取字段值<a class="headerlink" href="#getting-field-values" title="永久链接至标题">¶</a></h4>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">product</span><span class="p">[</span><span class="s1">&#39;name&#39;</span><span class="p">]</span>
<span class="go">Desktop PC</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">product</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;name&#39;</span><span class="p">)</span>
<span class="go">Desktop PC</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">product</span><span class="p">[</span><span class="s1">&#39;price&#39;</span><span class="p">]</span>
<span class="go">1000</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">product</span><span class="p">[</span><span class="s1">&#39;last_updated&#39;</span><span class="p">]</span>
<span class="gt">Traceback (most recent call last):</span>
    <span class="o">...</span>
<span class="gr">KeyError</span>: <span class="n">&#39;last_updated&#39;</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">product</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;last_updated&#39;</span><span class="p">,</span> <span class="s1">&#39;not set&#39;</span><span class="p">)</span>
<span class="go">not set</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">product</span><span class="p">[</span><span class="s1">&#39;lala&#39;</span><span class="p">]</span> <span class="c1"># getting unknown field</span>
<span class="gt">Traceback (most recent call last):</span>
    <span class="o">...</span>
<span class="gr">KeyError</span>: <span class="n">&#39;lala&#39;</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">product</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;lala&#39;</span><span class="p">,</span> <span class="s1">&#39;unknown field&#39;</span><span class="p">)</span>
<span class="go">&#39;unknown field&#39;</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;name&#39;</span> <span class="ow">in</span> <span class="n">product</span>  <span class="c1"># is name field populated?</span>
<span class="go">True</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;last_updated&#39;</span> <span class="ow">in</span> <span class="n">product</span>  <span class="c1"># is last_updated populated?</span>
<span class="go">False</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;last_updated&#39;</span> <span class="ow">in</span> <span class="n">product</span><span class="o">.</span><span class="n">fields</span>  <span class="c1"># is last_updated a declared field?</span>
<span class="go">True</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;lala&#39;</span> <span class="ow">in</span> <span class="n">product</span><span class="o">.</span><span class="n">fields</span>  <span class="c1"># is lala a declared field?</span>
<span class="go">False</span>
</pre></div>
</div>
</div>
<div class="section" id="setting-field-values">
<h4>设置字段值<a class="headerlink" href="#setting-field-values" title="永久链接至标题">¶</a></h4>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">product</span><span class="p">[</span><span class="s1">&#39;last_updated&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;today&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">product</span><span class="p">[</span><span class="s1">&#39;last_updated&#39;</span><span class="p">]</span>
<span class="go">today</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">product</span><span class="p">[</span><span class="s1">&#39;lala&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;test&#39;</span> <span class="c1"># setting unknown field</span>
<span class="gt">Traceback (most recent call last):</span>
    <span class="o">...</span>
<span class="gr">KeyError</span>: <span class="n">&#39;Product does not support field: lala&#39;</span>
</pre></div>
</div>
</div>
<div class="section" id="accessing-all-populated-values">
<h4>访问所有填充的值<a class="headerlink" href="#accessing-all-populated-values" title="永久链接至标题">¶</a></h4>
<p>要访问所有填充的值，只需使用 <a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#dict" title="(在 Python v3.9)"><code class="xref py py-class docutils literal notranslate"><span class="pre">dict</span></code></a> 应用程序编程接口：</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">product</span><span class="o">.</span><span class="n">keys</span><span class="p">()</span>
<span class="go">[&#39;price&#39;, &#39;name&#39;]</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">product</span><span class="o">.</span><span class="n">items</span><span class="p">()</span>
<span class="go">[(&#39;price&#39;, 1000), (&#39;name&#39;, &#39;Desktop PC&#39;)]</span>
</pre></div>
</div>
</div>
<div class="section" id="copying-items">
<span id="id4"></span><h4>复制项目<a class="headerlink" href="#copying-items" title="永久链接至标题">¶</a></h4>
<p>要复制项目，必须首先决定是要浅副本还是深副本。</p>
<p>如果您的物品包含 <a class="reference external" href="https://docs.python.org/3/glossary.html#term-mutable" title="(在 Python v3.9)"><span class="xref std std-term">mutable</span></a> 值如列表或字典，一个浅拷贝将在所有不同的拷贝中保持对相同可变值的引用。</p>
<p>例如，如果您有一个带有标记列表的项目，并且您创建了该项目的浅副本，那么原始项目和副本都具有相同的标记列表。向其中一个项目的列表中添加标记也会将标记添加到另一个项目中。</p>
<p>如果这不是所需的行为，请使用深度复制。</p>
<p>见 <a class="reference external" href="https://docs.python.org/3/library/copy.html#module-copy" title="(在 Python v3.9)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">copy</span></code></a> 更多信息。</p>
<p>要创建项目的浅副本，可以调用 <a class="reference internal" href="#scrapy.item.Item.copy" title="scrapy.item.Item.copy"><code class="xref py py-meth docutils literal notranslate"><span class="pre">copy()</span></code></a> 在现有项上 (<code class="docutils literal notranslate"><span class="pre">product2</span> <span class="pre">=</span> <span class="pre">product.copy()</span></code> ）或从现有项实例化项类 (<code class="docutils literal notranslate"><span class="pre">product2</span> <span class="pre">=</span> <span class="pre">Product(product)</span></code> ）</p>
<p>要创建深度复制，请调用 <a class="reference internal" href="#scrapy.item.Item.deepcopy" title="scrapy.item.Item.deepcopy"><code class="xref py py-meth docutils literal notranslate"><span class="pre">deepcopy()</span></code></a> 相反 (<code class="docutils literal notranslate"><span class="pre">product2</span> <span class="pre">=</span> <span class="pre">product.deepcopy()</span></code> ）</p>
</div>
<div class="section" id="other-common-tasks">
<h4>其他常见任务<a class="headerlink" href="#other-common-tasks" title="永久链接至标题">¶</a></h4>
<p>从项目创建dict：</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">dict</span><span class="p">(</span><span class="n">product</span><span class="p">)</span> <span class="c1"># create a dict from all populated values</span>
<span class="go">{&#39;price&#39;: 1000, &#39;name&#39;: &#39;Desktop PC&#39;}</span>
</pre></div>
</div>
<p>从dicts创建项目：</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">Product</span><span class="p">({</span><span class="s1">&#39;name&#39;</span><span class="p">:</span> <span class="s1">&#39;Laptop PC&#39;</span><span class="p">,</span> <span class="s1">&#39;price&#39;</span><span class="p">:</span> <span class="mi">1500</span><span class="p">})</span>
<span class="go">Product(price=1500, name=&#39;Laptop PC&#39;)</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">Product</span><span class="p">({</span><span class="s1">&#39;name&#39;</span><span class="p">:</span> <span class="s1">&#39;Laptop PC&#39;</span><span class="p">,</span> <span class="s1">&#39;lala&#39;</span><span class="p">:</span> <span class="mi">1500</span><span class="p">})</span> <span class="c1"># warning: unknown field in dict</span>
<span class="gt">Traceback (most recent call last):</span>
    <span class="o">...</span>
<span class="gr">KeyError</span>: <span class="n">&#39;Product does not support field: lala&#39;</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="extending-item-subclasses">
<h3>扩展项子类<a class="headerlink" href="#extending-item-subclasses" title="永久链接至标题">¶</a></h3>
<p>您可以通过声明原始项的子类来扩展项（添加更多字段或更改某些字段的元数据）。</p>
<p>例如：：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">DiscountedProduct</span><span class="p">(</span><span class="n">Product</span><span class="p">):</span>
    <span class="n">discount_percent</span> <span class="o">=</span> <span class="n">scrapy</span><span class="o">.</span><span class="n">Field</span><span class="p">(</span><span class="n">serializer</span><span class="o">=</span><span class="nb">str</span><span class="p">)</span>
    <span class="n">discount_expiration_date</span> <span class="o">=</span> <span class="n">scrapy</span><span class="o">.</span><span class="n">Field</span><span class="p">()</span>
</pre></div>
</div>
<p>您还可以通过使用前面的字段元数据并附加更多值或更改现有值来扩展字段元数据，如：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">SpecificProduct</span><span class="p">(</span><span class="n">Product</span><span class="p">):</span>
    <span class="n">name</span> <span class="o">=</span> <span class="n">scrapy</span><span class="o">.</span><span class="n">Field</span><span class="p">(</span><span class="n">Product</span><span class="o">.</span><span class="n">fields</span><span class="p">[</span><span class="s1">&#39;name&#39;</span><span class="p">],</span> <span class="n">serializer</span><span class="o">=</span><span class="n">my_serializer</span><span class="p">)</span>
</pre></div>
</div>
<p>添加（或替换）了 <code class="docutils literal notranslate"><span class="pre">serializer</span></code> 的元数据键 <code class="docutils literal notranslate"><span class="pre">name</span></code> 字段，保留所有以前存在的元数据值。</p>
</div>
</div>
<div class="section" id="supporting-all-item-types">
<span id="supporting-item-types"></span><h2>支持所有项目类型<a class="headerlink" href="#supporting-all-item-types" title="永久链接至标题">¶</a></h2>
<p>在接收项的代码中，例如 <a class="reference internal" href="item-pipeline.html#topics-item-pipeline"><span class="std std-ref">item pipelines</span></a> 或 <a class="reference internal" href="spider-middleware.html#topics-spider-middleware"><span class="std std-ref">spider middlewares</span></a> ，使用 <code class="xref py py-class docutils literal notranslate"><span class="pre">ItemAdapter</span></code> 类与 <code class="xref py py-func docutils literal notranslate"><span class="pre">is_item()</span></code> 函数来编写适用于任何 <a class="reference internal" href="#item-types"><span class="std std-ref">supported item type</span></a> ：</p>
</div>
<div class="section" id="other-classes-related-to-items">
<h2>与项目相关的其他类别<a class="headerlink" href="#other-classes-related-to-items" title="永久链接至标题">¶</a></h2>
<dl class="py class">
<dt id="scrapy.item.ItemMeta">
<em class="property">class </em><code class="sig-prename descclassname">scrapy.item.</code><code class="sig-name descname">ItemMeta</code><span class="sig-paren">(</span><em class="sig-param"><span class="n">class_name</span></em>, <em class="sig-param"><span class="n">bases</span></em>, <em class="sig-param"><span class="n">attrs</span></em><span class="sig-paren">)</span><a class="reference internal" href="../_modules/scrapy/item.html#ItemMeta"><span class="viewcode-link">[源代码]</span></a><a class="headerlink" href="#scrapy.item.ItemMeta" title="永久链接至目标">¶</a></dt>
<dd><p><a class="reference external" href="https://realpython.com/python-metaclasses">Metaclass</a> 属于 <a class="reference internal" href="#scrapy.item.Item" title="scrapy.item.Item"><code class="xref py py-class docutils literal notranslate"><span class="pre">Item</span></code></a> 它处理字段定义。</p>
</dd></dl>

</div>
</div>


           </div>
           
          </div>
          <footer>
  
    <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
      
        <a href="loaders.html" class="btn btn-neutral float-right" title="项目加载器" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right"></span></a>
      
      
        <a href="selectors.html" class="btn btn-neutral float-left" title="选择器" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left"></span> Previous</a>
      
    </div>
  

  <hr/>

  <div role="contentinfo">
    <p>
        
        &copy; 版权所有 2008–2020, Scrapy developers
      <span class="lastupdated">
        最后更新于 10月 18, 2020.
      </span>

    </p>
  </div>
    
    
    
    Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a
    
    <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a>
    
    provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
  
 
<script type="text/javascript">
!function(){var analytics=window.analytics=window.analytics||[];if(!analytics.initialize)if(analytics.invoked)window.console&&console.error&&console.error("Segment snippet included twice.");else{analytics.invoked=!0;analytics.methods=["trackSubmit","trackClick","trackLink","trackForm","pageview","identify","reset","group","track","ready","alias","page","once","off","on"];analytics.factory=function(t){return function(){var e=Array.prototype.slice.call(arguments);e.unshift(t);analytics.push(e);return analytics}};for(var t=0;t<analytics.methods.length;t++){var e=analytics.methods[t];analytics[e]=analytics.factory(e)}analytics.load=function(t){var e=document.createElement("script");e.type="text/javascript";e.async=!0;e.src=("https:"===document.location.protocol?"https://":"http://")+"cdn.segment.com/analytics.js/v1/"+t+"/analytics.min.js";var n=document.getElementsByTagName("script")[0];n.parentNode.insertBefore(e,n)};analytics.SNIPPET_VERSION="3.1.0";
analytics.load("8UDQfnf3cyFSTsM4YANnW5sXmgZVILbA");
analytics.page();
}}();

analytics.ready(function () {
    ga('require', 'linker');
    ga('linker:autoLink', ['scrapinghub.com', 'crawlera.com']);
});
</script>


</body>
</html>